from django.http import HttpResponse
from django.shortcuts import render
from utils.cawldata import CrawlData
from web import models



def data_crawl(request, project_id):
    crawl = CrawlData()
    fun_data_list = crawl.page_data()
    models.CrawlDate.objects.all().delete()
    serial = 0
    for fun in fun_data_list:
        fun_data_dic = []
        fun_list = fun[0:12]
        fun_list.pop(2)
        for item in fun_list:
            fun_data_dic.append("".join(item))
        serial += 1
        models.CrawlDate.objects.create(project_id=request.tracer.project.id, serial=serial, fund_code=fun_data_dic[0],
                                        fund_name=fun_data_dic[1],
                                        f_datetime=fun_data_dic[2],
                                        f_value=fun_data_dic[3], f_net_value=fun_data_dic[4],
                                        day_growth=fun_data_dic[5] + "%", week=fun_data_dic[6] + "%",
                                        january=fun_data_dic[7] + "%", march=fun_data_dic[8] + "%",
                                        six_month=fun_data_dic[9] + "%",
                                        year=fun_data_dic[10] + "%")
        data_list = models.CrawlDate.objects.filter(project_id=project_id)
    return render(request, "personal_management/data_crwal.html", {"data_list": data_list})


def keywordcrawl(request, project_id):
    data = request.GET["str"]

    return render(request, "personal_management/data_crwal.html")
